package miblab.school.background;

import java.io.IOException;
import com.techventus.server.voice.Voice;

import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.telephony.gsm.SmsManager;
import miblab.school.SystemFunctions;
import miblab.school.SystemParameters;
import miblab.school.views.ViewResults;

@SuppressWarnings("deprecation")
public class MyAlarmService extends Service {

	public ViewResults smsManager ;
	private String FN;
	private double[] timings = new double[4];
	private String SMS;
	private boolean msg_OK = true;

	@Override

	public void onCreate() {

	}

	@Override

	public IBinder onBind(Intent intent) {
		return null;

	}

	@Override

	public void onDestroy() {
		super.onDestroy();
	}

	@Override

	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
		FN= SystemFunctions.Getfilename();
		timings= SystemFunctions.ParseString(""+SystemParameters.path+"/School/"+FN);
		SMS=ConstructSMS(timings);
		if( msg_OK)
			try {
				sendSMS(SystemParameters.phno,SMS);
			} catch (Exception e) {
				e.printStackTrace();
			}
	}



	@Override
	public boolean onUnbind(Intent intent) {
		return super.onUnbind(intent);
	}

	//**********************Construct SMS************************************************
	public String ConstructSMS(double[] TS)
	{

		String text = "";
		text=text+"LOC*"+SystemParameters.ID+"&Home="+TS[0]+"&School="+TS[1]+"&Hospital="+TS[2]+"&Others="+TS[3];
		return text;
	}
	//**********************Send SMS************************************************


	public void sendSMS(String phoneNo, String message) throws IOException {


		if(SystemParameters.internet)
		{
			Voice voice = new Voice(SystemParameters.userName, SystemParameters.pass,"GoogleVoiceJava",true,Voice.GOOGLE);
			if(!voice.isLoggedIn())
			{
				voice.login();
			}

			voice.sendSMS(phoneNo, message);
		}
		else
		{
			PendingIntent pi = PendingIntent.getActivity(this, 0,
					new Intent(this,ViewResults.class), 0);                
			SmsManager sms = SmsManager.getDefault();
			sms.sendTextMessage(phoneNo, null, message, pi, null); 
		}   
	} 
	
}
